package com.bukaedu.info.manager.controller;

import com.bukaedu.info.manager.domain.Student;
import com.bukaedu.info.manager.domain.Teacher;
import com.bukaedu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        teacherLoop:

        while(true){
            System.out.println("--------欢迎来到<老师>管理系统--------");
            System.out.println("请输入您的选择：\n1.添加老师\n2.删除老师\n3.修改老师\n4.查看老师\n5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    addTeacher();
                break ;
                case "2":
                    deleteTeacherById();
                    break ;
                case "3":
                    updateTeacher();
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用<老师>管理系统，再见！");
                    break teacherLoop;
                default:
                    System.out.println("你的输入错误，重新输入！");
                    break;
            }
        }
    }

    private void deleteTeacherById() {
        String id = inputTeacherId();
        teacherService.deleteTeacherById(id);
        System.out.println("删除成功！");
    }

    private void updateTeacher() {
        String id = inputTeacherId();
        Teacher newTeacher = inputTeacherInfo(id);
        teacherService.updateTeacher(id,newTeacher);
        System.out.println("修改成功！");
    }

    private void addTeacher() {
        String id;
        while(true){
            System.out.println("请输入id：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if(exists){
                System.out.println("id已存在，请重新输入：");
            }else{
                break;
            }
        }
        Teacher t = inputTeacherInfo(id);

        boolean res = teacherService.addTeacher(t);
        if(res){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
    }

    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if(teachers == null){
            System.out.println("还没有数据，去添加~");
            return;
        }

        System.out.println("序号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if(t != null){
                System.out.println(t.getId() + "\t" +t.getName() +"\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }
    }

    public String inputTeacherId(){
        String id;
        while(true){
            System.out.println("请输入id：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if(!exists){
                System.out.println("您输入的id不存在，请重新输入！");
            }else{
                break;
            }
        }
        return id;
    }

    public Teacher inputTeacherInfo(String id){
        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        Teacher t = new Teacher(id,name,age,birthday);
        return  t;
    }
}
